<?php

class_exists('MPPage') || require('class.MPPage.php');
class_exists('MPOverview') || require('class.MPOverview.php');
class_exists('Database') || require('class.Database.php');
class_exists('Conflicts') || require('class.Conflicts.php');

class MPInterface
{
	private $m_name;
	private $m_description;
	private $m_stylesheet_url;
	private $m_logo_image_url;
	private $m_homedir;
	private $m_page;
	private $m_navbar_contents;

	public function __construct($name,$desc,$styleURL,$logoURL,$homeDir)
	{
		$this->m_name = $name;
		$this->m_description = $desc;
		$this->m_stylesheet_url = $styleURL;
		$this->m_logo_image_url = $logoURL;
		$this->m_homedir = $homeDir;

		$this->m_navbar_contents = <<<HTM
			<a href="?p=login">Login</a>
			&bull;
			<a href="?p=register">Register</a>
			&bull;
			<a href="?p=overview">Overview</a>
			&bull;
			<a href="?p=addvehicle">Add Vehicle</a>
			&bull;
			<a href="?p=addtrip">Add Trip</a>
			&bull;
			<a href="?p=conflicts">Conflicts</a>
			&bull;
			<a href="?p=free">Free Resources</a>
HTM;
	}

	public function try_get($get)
	{
		if( isset($get['p']) )
		{
			$page = $get['p'];
			$page_safe = filter_var( $page, FILTER_SANITIZE_STRING );
			switch( $page_safe )
			{
				// ---------- ABOUT ----------------------------------------
				case "about":
					$this->m_page = new MPPage("About","This is where you tell the user what the service is for, why they should use it, that sort of thing.");
					break;
					
				case "register":
					$htm = <<<HTM
			<form action="register.php" method="post">
				<input class="greyout" type="text" name="name" value="" /> Company Name<br/>
				<input class="greyout" type="password" name="pass" value="" /> Password<br/>
				<input class="greyout" type="password" name="passconf" value="" /> Repeat Password<br/>
				<input type="submit" value="Submit" />
			</form>
HTM;
					$this->m_page = new MPPage("Register",$htm);
					break;
				// ---------- LOGIN ----------------------------------------
				case "login":
					$htm = <<<HTM
			<form action="login.php" method="post">
				<input class="greyout" type="text" name="name" value="" /> Company Name<br/>
				<input class="greyout" type="password" name="pass" value="" /> Password<br/>
				<input type="submit" value="Submit" />
			</form>
HTM;
					$this->m_page = new MPPage("Login",$htm);
					break;
				// ---------- ADD VEHICLE ----------------------------------------
				case "addvehicle":
					//get company from session
					$htm = <<<HTM
			<form action="addVehicle.php" method="post">
				<input class="greyout" type="text" name="reg" value="" /> Registration<br/>
				<input class="greyout" type="text" name="cap" value="" /> Capacity<br/>
				<input class="greyout" type="text" name="whcap" value="" /> WheelChair Capacity<br/>
				<input class="greyout" type="text" name="com" value="" /> Company<br/>
				<input type="submit" value="Submit" />
			</form>
HTM;
					$this->m_page = new MPPage("Add Vehicle",$htm);
					break;
				// ---------- ADD TRIP ----------------------------------------
				case "addtrip":
					$htm = <<<HTM
			<form action="addTrip.php" method="post">
				<input class="greyout" type="text" name="org" value="" /> Origin<br/>
				<input class="greyout" type="text" name="dest" value="" /> Destination<br/>
				<input class="greyout" type="test" name="date" value="" /> Date (YYYY-MM-DD) <br/>
				<input class="greyout" type="text" name="driver" value="" /> Driver<br/>
				<input class="greyout" type="text" name="vehicle_reg" value="" /> Vehicle_Registration<br/>
				<input type="submit" value="Submit" />
			</form>
HTM;
					$this->m_page = new MPPage("Add Trip",$htm);
					break;
				// ---------- FREE RESOURCE ----------------------------------------
				case "free":
					$htm = "";
					$this->m_page = new MPPage("Free",$htm);
					break;
				// ---------- OVERVIEW ----------------------------------------
				case "overview":
					$overview = new MPOverview(1);
					$overview->load_from_db(new Database());
					$this->m_page = new MPPage("Overview",$overview->get_html());
					break;
				// ---------- CONFLICTS ----------------------------------------
				case "conflicts":
					$conflicts = new Conflicts();
					$this->m_page = new MPPage("Conflicts",$conflicts->get_html());
					break;
				// ---------- ERROR ----------------------------------------
				default:
					$this->m_page = new MPPage("Error","<h1>Error</h1><p>Requested page could not be matched.</p>");
					break;
			}
		}
		else
		{
			$this->m_page = new MPPage("","<h1>Welcome</h1><p>This website matches up vehicles that have spare capacity with groups that want to make a trip.");
		}
	}

	public function display()
	{
		$output_html = "<!DOCTYPE html>";

		$pageTitle = $this->m_page->get_title();
		$output_html .= <<<HTM

<html>
	<head>
		<title>$this->m_name - $this->m_description - $pageTitle</title>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="$this->m_stylesheet_url" />
	</head>
	
	<body>
		<div id="wrapper">
		<div class="colour-primary" id="header"><div class="container">
		<a href="$this->m_homedir"><img src="$this->m_logo_image_url" alt="Logo" title="$this->m_name" /></a>
		</div></div>
		<div class="colour-secondary" id="navbar"><div class="container">
		$this->m_navbar_contents
		</div></div>
		<div id="content"><div class="container">

HTM;

		$output_html .= $this->m_page->get_html();

		$names = "Ross Henderson, Jack Miller and Jonathon Higgs";

		$output_html .= <<<HTM

		</div></div>
		<div class="colour-primary" id="footer"><div class="container">
			<a href="?p=about">About</a>
			<span style="float:right;font-style:italic;">Developed by $names</span>
		</div></div>
		</div>
	</body>
</html>
HTM;

		echo $output_html;
	}
}

?>